package com.bkhc.partner.api;

import com.bkhc.common.annotation.*;
import com.bkhc.common.enu.OperationEnum;
import com.bkhc.common.util.ExcelUtil;
import com.bkhc.common.util.NoModleDataListener;
import com.bkhc.common.util.response.ResultJson;
import com.bkhc.partner.bo.PriceSchame;
import com.bkhc.partner.entity.Partner;
import com.bkhc.partner.service.PartnerSerivce;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;

/**
 * @author pcq
 * @date 2021/6/20 17:57
 */
@RestController
@RequestMapping("/manage")
@Api("厂商管理服务")
@ApiPackage(code="partner-api",name="厂商管理服务",description = "后台厂商管理服务操作接口")
public class PartnerController extends CommonController{
    private static final Logger logger = LoggerFactory.getLogger(PartnerController.class);
    @Autowired
    private PartnerSerivce partnerSerivce;

    @ApiOperation(code="addPartner",name="添加厂商",description = "添加厂商",type = "common")
    @AuditLog(object="partner",operation = "添加厂商",type = OperationEnum.query)
    @RequestMapping(path = "/addPartner",method = RequestMethod.POST)
    public ResultJson<?> addPartner(@RequestBody(required=true) Partner partner) {
        ResultJson<?> res = partnerSerivce.addPartner(partner);
        return res;
    }

    @ApiOperation(code="deletePartner",name="删除厂商",description = "删除厂商",type = "common")
    @AuditLog(object="deletePartner",operation = "删除厂商",type = OperationEnum.query)
    @RequestMapping(path = "/deletePartner/{partnerCode}",method = RequestMethod.POST)
    public ResultJson<?> addPartner(@PathVariable(name="partnerCode") String partnerCode) {
        ResultJson<?> res = partnerSerivce.deletePartner(partnerCode);
        return res;
    }

    @ApiOperation(code="queryBrandProductByUser",name="根据登录用户查询厂商品牌",description = "根据用户查询厂商品牌")
    @AuditLog(object="queryBrandProductByUser",operation = "根据登录用户查询厂商品牌",type = OperationEnum.query)
    @RequestMapping(path = "/getBrandProduct/{pageNum}/{pageSize}",method = RequestMethod.GET)
    public ResultJson<?> getBrand(@PathVariable(required=false,name="pageNum") int pageIndex, @PathVariable(required=false,name="pageSize") int pageSize) {
        ResultJson<?> res = partnerSerivce.getBrandProduct(pageIndex,pageSize);
        return res;
    }

    @ApiOperation(code="queryProductByBrand",name="根据登录用户查询厂商品牌",description = "根据用户查询厂商品牌",type = "common")
    @AuditLog(object="queryProductByBrand",operation = "根据登录用户查询厂商品牌",type = OperationEnum.query)
    @RequestMapping(path = "/queryProductByBrand",method = RequestMethod.GET)
    public ResultJson<?> getBrand(@RequestParam(required=true,name="brandCode") String  brandCode) {
        ResultJson<?> res = partnerSerivce.queryProductByBrand(brandCode);
        return res;
    }

    @ApiOperation(code="getProductSpec",name="查询产品规格",description = "查询产品规格",type = "common")
    @AuditLog(object="getProductSpec",operation = "查询产品规格",type = OperationEnum.query)
    @RequestMapping(path = "/getProductSpec",method = RequestMethod.GET)
    public ResultJson<?> getProductSpec(@RequestParam(required=true,name="productCode") String  productCode) {
        ResultJson<?> res = partnerSerivce.getProductSpec(productCode);
        return res;
    }

    @ApiOperation(code="getBrandProductGood",name="查询品牌产品商品",description = "查询品牌产品商品",type = "common")
    @AuditLog(object="getBrandProductGood",operation = "查询品牌产品商品",type = OperationEnum.query)
    @RequestMapping(path = "/getBrandProductGood",method = RequestMethod.GET)
    public ResultJson<?> getBrandProductGood(@RequestParam(required=true,name="productCode") String  productCode,@RequestParam(required=true,name="brandCode") String  brandCode) {
        ResultJson<?> res = partnerSerivce.getBrandProductGood(productCode,brandCode);
        return res;
    }

    @ApiOperation(code="getUploadPriceRule",name="查询品牌产品商品价格上传规则",description = "查询品牌产品商品",type = "common")
    @AuditLog(object="getUploadPriceRule",operation = "查询品牌产品商品价格上传规则",type = OperationEnum.query)
    @RequestMapping(path = "/getUplodaPriceRule",method = RequestMethod.GET)
    public ResultJson<?> getUploadPriceRule(@RequestParam(required=true,name="productCode") String  productCode,@RequestParam(required=true,name="brandCode") String  brandCode) {
        ResultJson<?> res = partnerSerivce.getUploadPriceRule(productCode,brandCode);
        return res;
    }

    @ApiOperation(code="queryUploadPrice",name="查询品牌产品商品价上传的价格",description = "查询品牌产品商品",type = "common")
    @AuditLog(object="queryUploadPrice",operation = "查询品牌产品商品价上传的价格",type = OperationEnum.query)
    @RequestMapping(path = "/queryUploadPrice",method = RequestMethod.GET)
    public ResultJson<?> queryUploadPrice(@RequestParam(required=true,name="productCode") String  productCode,@RequestParam(required=true,name="brandCode") String  brandCode) {
        ResultJson<?> res = partnerSerivce.queryUploadPrice(productCode,brandCode);
        return res;
    }

    @ApiOperation(code="getUploadFileSchame",name="获取品牌产品上传价格模板",description = "查询品牌产品商品",type = "common")
    @AuditLog(object="getUploadFileSchame",operation = "获取品牌产品上传价格模板",type = OperationEnum.query)
    @RequestMapping(path = "/getUploadFileSchame",method = RequestMethod.GET)
    public void getUploadFileSchame(@RequestParam(required=true,name="productCode") String  productCode,@RequestParam(required=true,name="brandCode") String  brandCode) {
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        Map<String, Object> product = (Map<String, Object>) partnerSerivce.getProduct(productCode).getData();
        Map<String, Object> brand = (Map<String, Object>) partnerSerivce.getBrand(brandCode).getData();
        List<String> head = partnerSerivce.getHead(product, brandCode);
        List<Map<String, Object>> goodsdata = partnerSerivce.getData(productCode, brandCode);
        List<List<Object>> data = new ArrayList<>();
        List<Map<String, String>> speclist = (List) product.get("productSpecList");
        int specSize = speclist.size();
        for (Map<String, Object> goods : goodsdata) {
            List<Object> line = new ArrayList<>();
            int index = 0;
            line.add(brand.get("brandName"));
            index = index + 1;
            line.add(product.get("productName"));
            List<Map<String, String>> goodspecList = (List<Map<String, String>>) goods.get("goodsSpecList");
            for (int i = 0; i < specSize; i++) {
                String specName = head.get(index + 1);
                index = index + 1;
                for (Map<String, String> goodspec : goodspecList) {
                    if (goodspec.get("specName").toString().equals(specName)) {
                        line.add(goodspec.get("specValue"));
                        break;
                    }
                }
            }
            for(;index<head.size();index++){
                line.add("");
            }
            data.add(line);
        }
        response.setContentType("multipart/form-data");
        response.setHeader("Content-Disposition", "attachment;fileName=priceSchame.xlsx");
        ServletOutputStream out = null;
        try {
            out = response.getOutputStream();
            ExcelUtil.writeExcelNoMapping(data, out, head);
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }

    @ApiOperation(code="uploadPrice",name="",description = "上传价格文件",type = "common")
    @AuditLog(object="file",operation = "上传价格文件",type = OperationEnum.add)
    @RequestMapping(path = "/uploadPrice",method = RequestMethod.POST)
    public ResultJson<?> uploadPrice(@RequestParam("file") MultipartFile file,@RequestParam(required=true,name="productCode") String  productCode,@RequestParam(required=true,name="brandCode") String  brandCode) throws IOException {
       List<List<String>> result = (List<List<String>>) ExcelUtil.excelRead(file.getInputStream());
        partnerSerivce.parsePrice(result,brandCode,productCode);
        ResultJson<?> resultJson = ResultJson.success();
        return resultJson;
    }

}
